package com.edu.web.servlet;

import com.alibaba.fastjson2.JSON;
import com.edu.entity.Rentals;
import com.edu.service.RentalsService;
import com.edu.service.imlp.RentalsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/addRentalServlet")
public class addRentalServlet extends HttpServlet {
    RentalsService rentalsService = new RentalsServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取id
        String uId = req.getParameter("uId");
        String vId = req.getParameter("vId");
        String duration = req.getParameter("duration");
        String totalCost = req.getParameter("totalCost");
        System.out.println(uId+","+vId+","+duration+","+totalCost);
        int a = Integer.parseInt(uId);
        int b = Integer.parseInt(vId);
        int c = Integer.parseInt(duration);
        int d =Integer.parseInt(totalCost);
        //查询
        boolean b1 = rentalsService.addRental(a, b, c, d);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("meg",b1);
        //响应
        String jsonString = JSON.toJSONString(map);
        PrintWriter writer = resp.getWriter();
        writer.print(jsonString);
        writer.flush();
        writer.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
